package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.WYRoleDaoI;
import sy.model.WYRole;
@Repository("wyRoleDao")
public class WYRoleDaoImpl implements WYRoleDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void save(WYRole wyRole) {
		// TODO Auto-generated method stub
		this.getCurrentSession().save(wyRole);
	}

	@Override
	public void deleteByIds(List<String> ids) {
		// TODO Auto-generated method stub
		String hql = "delete from WYRole where id in :ids";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameterList("ids", ids);
		query.executeUpdate();
	}

	@Override
	public void modify(WYRole wyRole) {
		// TODO Auto-generated method stub
		this.getCurrentSession().update(wyRole);
	}

	@Override
	public WYRole getWYRoleById(String id) {
		// TODO Auto-generated method stub
		String hql = "from WYRole where id = :id";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("id",id);
		List<WYRole> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WYRole> listWYRole() {
		// TODO Auto-generated method stub
		return this.getCurrentSession().createQuery("from WYRole").list();
	}
}
